<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
 * See the file COPYRIGHT.html for more details.
 */
 
require_once("../shared/global_constants.php");
require_once("../classes/School.php");
require_once("../classes/Query.php");

/******************************************************************************
 * MemberQuery data access component for library members
 *
 * @author David Stevens <dave@stevens.name>;
 * @version 1.0
 * @access public
 ******************************************************************************
 */
class SchoolQuery extends Query {
  var $_itemsPerPage = 1;
  var $_rowNmbr = 0;
  var $_currentRowNmbr = 0;
  var $_currentPageNmbr = 0;
  var $_rowCount = 0;
  var $_pageCount = 0;

  function setItemsPerPage($value) {
    $this->_itemsPerPage = $value;
  }
  function getCurrentRowNmbr() {
    return $this->_currentRowNmbr;
  }
  function getRowCount() {
    return $this->_rowCount;
  }
  function getPageCount() {
    return $this->_pageCount;
  }

  /****************************************************************************
   * Executes a query
   * @param string $type one of the global constants
   *               OBIB_SEARCH_BARCODE or OBIB_SEARCH_NAME
   * @param string $word String to search for
   * @param integer $page What page should be returned if results are more than one page
   * @access public
   ****************************************************************************
   */
  function execSearch($type, $word, $page) {
    # reset stats
    $this->_rowNmbr = 0;
    $this->_currentRowNmbr = 0;
    $this->_currentPageNmbr = $page;
    $this->_rowCount = 0;
    $this->_pageCount = 0;

    # Building sql statements
    if ($type == OBIB_SEARCH_CUE) {
      $col = "cue";
      $colType = "%Q ";
      $word = $word."%";
    } elseif ($type == OBIB_SEARCH_NAME) {
      $col = "name";
      $colType = "%Q ";
      $word = "%".$word."%";
    } elseif ($type == OBIB_SEARCH_CITY) {
      $col = "cityid";
      $colType = "%N ";
    }

    # Building sql statements
    $sql = $this->mkSQL("from school where %C like ".$colType, $col, $word);
    $sqlcount = "select count(*) as rowcount ".$sql;
    $sql = "select * ".$sql;
    $sql .= " order by cue";
    # setting limit so we can page through the results
    $offset = ($page - 1) * $this->_itemsPerPage;
    $limit = $this->_itemsPerPage;
    $sql .= $this->mkSQL(" limit %N, %N ", $offset, $limit);
    #echo "sql=[".$sql."]<br>\n";

    # Running row count sql statement
    $rows = $this->exec($sqlcount);
    if (count($rows) != 1) {
      Fatal::internalError("Wrong number of count rows");
    }
    # Calculate stats based on row count
    $this->_rowCount = $rows[0]["rowcount"];
    $this->_pageCount = ceil($this->_rowCount / $this->_itemsPerPage);

    # Running search sql statement
    $this->_exec($sql);
  }

  /****************************************************************************
   * Executes a query
   * @param string $schoolid Member id of library member to select
   * @return boolean returns false, if error occurs
   * @access public
   ****************************************************************************
   */
  function get($schoolid) {
    $sql = $this->mkSQL("select * from school "
                        . "where schoolid=%N ", $schoolid);
    $rows = $this->exec($sql);
    if (count($rows) != 1) {
      Fatal::internalError("Bad schoolid");
    }
    return $this->_mkObj($rows[0]);
  }
  
  function maybeGetByName($name) {
    $sql = $this->mkSQL("select * from school "
                        . "where barcode_nmbr=%Q ", $bcode);
    $row = $this->select01($sql);
    if($row)
      return $this->_mkObj($row);
    return NULL;
  }

  /****************************************************************************
   * Fetches a row from the query result and populates the School object.
   * @return School returns library member or false if no more members to fetch
   * @access public
   ****************************************************************************
   */
  function fetchSchool() {
    $array = $this->_conn->fetchRow();
    if ($array == false) {
      return false;
    }
    # increment rowNmbr
    $this->_rowNmbr = $this->_rowNmbr + 1;
    $this->_currentRowNmbr = $this->_rowNmbr + (($this->_currentPageNmbr - 1) * $this->_itemsPerPage);
    return $this->_mkObj($array);
  }
  function _mkObj($array) {
    $school = new School();
    $school->setSchoolId($array["schoolid"]);
    $school->setAddress($array["address"]);
    $school->setAnexee($array["anexee"]);
    $school->setAreaId($array["areaid"]);
    $school->setCityId($array["cityid"]);
    $school->setCue($array["cue"]);
    $school->setDepartamentId($array["departamentid"]);
    $school->setEst($array["est"]);
    $school->setName($array["name"]);
    $school->setPhone($array["phone"]);
    $school->setStateId($array["stateid"]);

    return $school;
  }
  
  /****************************************************************************
   * Returns true if barcode number already exists
   * @param string $barcode Library member barcode number
   * @param string $schoolid Library member id
   * @return boolean returns true if barcode already exists
   * @access private
   ****************************************************************************
   */
  function DupCue($cue, $schoolid=0) {
    $sql = $this->mkSQL("select count(*) as num from school "
                        . "where cue = %Q and schoolid <> %N",
                        $cue, $schoolid);
    $rows = $this->exec($sql);
    if (count($rows) != 1) {
      Fatal::internalError('Bad number of rows');
    }
    if ($rows[0]['num'] > 0) {
      return true;
    }
    return false;
  }

  /****************************************************************************
   * Inserts a new library member into the member table.
   * @param School $school library member to insert
   * @return integer the id number of the newly inserted member
   * @access public
   ****************************************************************************
   */
  function insert($school) {
    $sql = $this->mkSQL("insert into school "
                        . "(schoolid,  "
                        . " stateid, cue, anexee, areaid, "
                        . " est, name, address, phone, cityid, departamentid) "
                        . "values (null, %N, %Q, "
                        . " %Q, %N, %N, %Q, %Q, %Q, %N, %N) ",
                        $school->getStateId(), $school->getCue(),
                        $school->getAnexee(), $school->getAreaId(),
                        $school->getEst(), $school->getName(),
                        $school->getAddress(), $school->getPhone(),
                        $school->getCityId(), $school->getDepartamentId());

    $this->exec($sql);
    $schoolid = $this->_conn->getInsertId();
    return $schoolid;
  }

  /****************************************************************************
   * Update a library member in the member table.
   * @param School $school library member to update
   * @access public
   ****************************************************************************
   */
  function update($school) {
    $sql = $this->mkSQL("update school set "
                        . " stateid = %N, cue=%Q, "
                        . " anexee=%Q,  areaid=%N,  est=%N, "
                        . " name=%Q, address=%Q, phone=%Q, "
                        . " cityid=%Q, departamentid=%N "
                        . "where schoolid=%N",
                        $school->getStateId(), $school->getCue(),
                        $school->getAnexee(), $school->getAreaId(),
                        $school->getEst(), $school->getName(),
                        $school->getAddress(), $school->getPhone(),
                        $school->getCityId(), $school->getDepartamentId(),
						$school->getSchoolId());

    $this->exec($sql);
  }

  /****************************************************************************
   * Deletes a library member from the member table.
   * @param string $schoolid Member id of library member to delete
   * @access public
   ****************************************************************************
   */
  function delete($schoolid) {
    $sql = $this->mkSQL("delete from school where schoolid = %N ", $schoolid);
    $this->exec($sql);
  }
}

?>
